<template>
    <div class="container">
        <div class="search-container">
            <el-input
                    placeholder="默认展示部分用户，可以通过用户名搜索用户..."
                    prefix-icon="el-icon-search" v-model="keywords" size="small">
            </el-input>
            <el-button type="primary" icon="el-icon-search" size="small" @click="searchClick">搜索</el-button>
        </div>
        <div class="user-card-list" >
            <el-card v-for="(user,index) in users" :key="index" v-loading="loading">
                <div slot="header" class="card-header">
                    <span>{{user.nickName}}</span>
                    <el-button type="text" icon="el-icon-delete" @click="deleteUser(user)">删除</el-button>
                </div>
                <div class="card-body">
                    <img :src="user.photo" :alt="user.nickName">
                    <div>用户名：{{user.name}}</div>
                    <div>电子邮箱：{{user.email}}</div>
                    <div>注册时间：{{user.regTime|formatDateTime}}</div>
                    <div>
                        <span>用户状态：</span>
                        <el-switch v-model="user.enable" active-text="启用" inactive-text="禁用"
                                   active-color="#13ce66" @change="enableChange(user.enable,user.id)">
                        </el-switch>
                    </div>
                    <div class="roles-container">
                        <span>用户角色：</span>
                        <el-tag type="success" size="mini" v-for="(role,index) in user.roles" :key="index">
                            {{role.name}}
                        </el-tag>
                        <el-popover placement="right" title="角色列表"
                                    @hide="updateRoles(user.id)">
                            <el-select v-model="roles" multiple placeholder="请选择" size="mini">
                                <el-option v-for="role in allRoles" :key="user.id+'-'+role.id"
                                           :label="role.name" :value="role.id"></el-option>
                            </el-select>
                            <el-button type="text" icon="el-icon-more" slot="reference"
                                       @click="showRolePop(user.roles)"></el-button>
                        </el-popover>
                    </div>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
    import {deleteReq, get, put} from "../utils/api";

    export default {
        data() {
            return {
                loading: false,
                keywords: '',
                users: [],
                roles: [],
                tempRoles: [],
                allRoles: [],
            }
        },
        mounted() {
            this.loadUserList();
            this.loadAllRoles();
        },
        methods: {
            searchClick() {
                this.loadUserList();
            },
            loadUserList() {
                this.loading = true;
                get('/admin/users', {nickName: this.keywords}).then(res => {
                    this.loading = false;
                    if (res.status === 0) {
                        this.users = res.data;
                    }
                })
            },

            loadAllRoles() {
                get('/admin/allRoles').then(res => {
                    if (res.status === 0) {
                        this.allRoles = res.data;
                    }
                })
            },

            showRolePop(roles) {
                console.log(roles);
                this.roles = [];
                roles.forEach(it => {
                    this.roles.push(it.id);
                });
                this.tempRoles = this.roles;
            },

            updateRoles(userId) {
                // 判断是否有操作
                if (this.roles === this.tempRoles) {
                    return;
                }
                console.log(this.roles);
                put("/admin/user/updateRoles", {
                    roleIds: this.roles, userId: userId
                }).then(res => {
                    console.log(res);
                    if (res.status === 0) {
                        this.$message.success(res.msg);
                        this.loadUserList();
                    } else {
                        this.$message.error(res.msg);
                    }
                })
            },

            /**
             * 启用和禁用用户
             * @param enable 用户启用状态
             * @param userId 用户ID
             */
            enableChange(enable, userId) {
                put("/admin/user/enable", {enable: enable, userId: userId}).then(res => {
                    if (res.status === 0) {
                        this.$message.success(res.msg);
                    } else {
                        this.$message.error(res.msg);
                    }
                });
            },

            /**
             * 删除用户
             * @param user 用户
             */
            deleteUser(user) {
                this.$confirm('确认删除文章\t' + user.nickName + '\t?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteReq("admin/user/deleteUserById", {userId: user.id}).then(res => {
                        if (res.status === 0) {
                            this.$message.success(res.msg);
                            this.loadUserList();
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                });
            }
        }
    }
</script>

<style scoped lang="stylus">
    .container {
        .search-container {
            margin-top 20px
            display flex
            justify-content center

            .el-input {
                width 300px
                margin-right 4px
            }
        }

        .user-card-list {
            display flex
            flex-wrap wrap
            margin-top 20px

            .el-card {
                width 300px
                margin 10px

                .card-header {
                    display flex
                    justify-content space-between
                    align-items center
                    height 15px

                    .el-button {
                        color #ff0509
                    }
                }

                .card-body {
                    display flex
                    flex-direction column
                    text-align left
                    line-height 1.5rem
                    font-size 0.9rem
                    color: #20a0ff

                    img {
                        height 70px
                        width 70px
                    }

                    .roles-container {
                        .el-tag {
                            margin 0 2px
                        }
                    }
                }
            }
        }
    }

</style>
